<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
     <style>
        body{
            height: 2000px;
        }
        .nav{
            height: 60px;
            width: 100%;
            background: transparent;
            position: fixed;
            top: 0;
            left: 0;
        }
    </style>
</head>
<body>
    <div class="nav" id="nav">导航</div>
   <script>
       /* onscroll  滚动事件 */
       var nav=document.getElementById("nav")
       window.onscroll=function(){
           /* 获取滚动条距离顶部的高度 */
            var scrollTop=document.documentElement.scrollTop;
            /* 当滚动条距离顶部的高度达到300的时候完全显示 */
            var opacity=scrollTop/300;
            if(opacity>1){
                opacity=1;
            }
            nav.style.opacity=opacity;
            nav.style.backgroundColor="pink";
       }   
       </script>
</body>
</html>